<%--
  -	Date: 2012-05-22
  -
  - Copyright (C) 2012 Jawa Software. All rights reserved.
  - 
  - 用户管理页面。
--%>
<%@page
	import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<%@ page
	import="org.jawa.learning.webapp.util.*,org.jawa.core.util.*,org.jawa.core.JawaServer,org.jawa.learning.model.User,org.jawa.learning.service.UserManager"%>
<%@ page
	import="java.util.Collection,java.util.List,java.util.ArrayList"%>
<%@ page import="org.apache.commons.lang.StringUtils"%>

<jsp:useBean id="webManager" class="org.jawa.learning.webapp.util.WebManager" />
<%
	webManager.init(request, response, session, application, out);
	String path = request.getContextPath();
%>

<%
	Page userPage = new Page();
	int range = ParamUtils.getIntParameter(request, "range", 10);
	int start = webManager.getPageIndex(range, "user");

	userPage.setPageSize(range);
	userPage.setPageNo(start);

	ApplicationContext ctx = WebApplicationContextUtils
			.getWebApplicationContext(session.getServletContext());
	UserManager userManager = (UserManager) ctx.getBean("userManager");

	String brId = ParamUtils.getParameter(request, "brId");
	String userName = ParamUtils.getParameter(request, "userName");
	String fullName = ParamUtils.getParameter(request, "fullName");

	if ( true || ( request.getParameter("search") != null && !request.getParameter("search").equals("") ) ) 
	{

		User user = new User();
		if (StringUtils.isNotEmpty(brId)) {
			user.setBrId(Long.valueOf(brId));
		}
		if (StringUtils.isNotEmpty(userName)) {
			user.setUsername(userName);
		}
		if (StringUtils.isNotEmpty(fullName)) {
			user.setFullName(fullName);
		}
		userPage = userManager.getUsers(user, userPage);
	}

	request.setAttribute("users", userPage.getResult());
	request.setAttribute("userCount", userPage.getTotalItems());
	request.setAttribute("range", userPage.getPageSize());
%>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>用户管理</title>
		<meta name="navID" content="1000" />
		<meta name="pageID" content="1301" />
	</head>
	<body>
		<div class="container-fluid">
			<div class="page-header ">
				<h4>
					用户管理
				</h4>
			</div>
			<form action="index.jsp" method="post" class="form-search well ">
					<label class="query-label">
						用户名:
					</label>
					<input type="text" name="userName" value="<%=userName==null?"":userName%>" class="input-small ">
					<label class="query-label">
						姓名:
					</label>
					<input type="text" name="fullName" value="<%=fullName==null?"":fullName%>"
						class="input-small ">
					<input type="submit" name="search" class="btn btn-primary"
						value="查询">
			</form>

			<div class="row-fluid">
				<div class="pull-left">
					<%
						if ("true".equals(request.getParameter("create"))
								&& "true".equals(request.getParameter("success"))) {
					%>
					<p class="alert alert-success">
						<a class="close" data-dismiss="alert" href="#">×</a> 创建新用户成功!
					</p>
					<%
						}
					%>
					<%
						if ("true".equals(request.getParameter("update"))
								&& "true".equals(request.getParameter("success"))) {
					%>
					<p class="alert alert-success">
						<a class="close" data-dismiss="alert" href="#">×</a> 修改用户成功!
					</p>
					<%
						}
					%>
					<%
						if ("true".equals(request.getParameter("delete"))
								&& "true".equals(request.getParameter("success"))) {
					%>
					<p class="alert alert-success">
						<a class="close" data-dismiss="alert" href="#">×</a> 删除用户成功!
					</p>
					<%
						}
					%>
				</div>
				<div class="btn-toolbar pull-right">
					<a class="btn btn-success " href="user-form.jsp"><i class="icon-user icon-white"></i>增加用户</a>
				</div>
			</div>

			<display:table id="user" name="users" partialList="true" size="${userCount}" pagesize="${range}" requestURI="index.jsp"
				export="false" class="table table-striped table-bordered table-condensed">
				<display:column title="用户名">
					<c:out value="${user.username}" />
				</display:column>
				<display:column title="姓名">
					<c:out value="${user.fullName}" />
				</display:column>
				<display:column title="类型">
					<c:if test="${user.userType=='1'}">一般用户</c:if>
					<c:if test="${user.userType=='2'}">管理用户</c:if>
				</display:column>
				<display:column title="状态">
					<c:if test="${user.enabled=='0'}">正常</c:if>
					<c:if test="${user.enabled=='1'}">禁用</c:if>
				</display:column>
				<display:column title="操作" style="width:15%;">
					<div class="btn-group">
						<a href="#" onclick="return deleteUser('${user.id }');"
							class="btn btn-mini"><i class="icon-trash "></i>删除</a>
						<a href="user-form.jsp?edit=true&userId=${user.id}"
							class="btn btn-mini"><i class="icon-edit "></i>修改</a>
					</div>
				</display:column>
			</display:table>

		</div>

		<script>
			function deleteUser(userId) {
				if (confirm('你确定要删除当前用户吗?')) {
					window.location.href = 'user-form.jsp?delete=true&userId=' + userId;
				}
			}
		</script>

	</body>